package com.cmti.skeleton.dao.mapper;

import com.cmti.skeleton.dto.SysUserDto;
import com.cmti.skeleton.entity.SysUser;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.type.JdbcType;

public interface SysUserMapper {

    @Delete({
        "delete from sys_user",
        "where ID = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Delete({
        "delete from sys_user_role",
        "where USER_ID = #{uid}"
    })
    int deleteUserRoles(long uid);

    @Insert({
        "insert into sys_user (ACCOUNT, PASSWORD, ",
        "NAME, SEX, DEPT_ID, ",
        "CERT_TYPE, CERT_NO, ",
        "TEL, MOBILE, EMAIL, ",
        "CREATE_TIME, EXP_TIME, ",
        "STATUS, LOGIN_NUM, ",
        "REMARK)",
        "values (#{account,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, ",
        "#{name,jdbcType=VARCHAR}, #{sex,jdbcType=TINYINT}, #{deptId,jdbcType=BIGINT}, ",
        "#{certType,jdbcType=INTEGER}, #{certNo,jdbcType=VARCHAR}, ",
        "#{tel,jdbcType=VARCHAR}, #{mobile,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR}, ",
        "#{createTime,jdbcType=TIMESTAMP}, #{expTime,jdbcType=TIMESTAMP}, ",
        "#{status,jdbcType=INTEGER}, #{loginNum,jdbcType=VARCHAR}, ",
        "#{remark,jdbcType=VARCHAR})"
    })
    @SelectKey(statement = "SELECT LAST_INSERT_ID()", keyProperty = "id", before = false, resultType = Long.class)
    int insert(SysUser record);

    @InsertProvider(type = SysUserSqlProvider.class, method = "insertSelective")
    @SelectKey(statement = "SELECT LAST_INSERT_ID()", keyProperty = "id", before = false, resultType = Long.class)
    int insertSelective(SysUser record);

    @InsertProvider(type = SysUserSqlProvider.class, method = "insertUserRoles")
    int insertUserRoles(SysUserDto dto);

    @Insert({
        "insert into sys_user_role(ROLE_ID,USER_ID) VALUES",
        "(#{rid}, #{uid})"
    })
    int insertUserRole(@Param("uid") long uid, @Param("rid") long rid);

    @Select({
        "select",
        "ID, ACCOUNT, PASSWORD, NAME, SEX, DEPT_ID, CERT_TYPE, CERT_NO, TEL, MOBILE, ",
        "EMAIL, CREATE_TIME, EXP_TIME, STATUS, LOGIN_NUM, REMARK",
        "from sys_user",
        "where ID = #{id,jdbcType=BIGINT}"
    })
    @Results({
        @Result(column = "ID", property = "id", jdbcType = JdbcType.BIGINT, id = true)
        ,
        @Result(column = "ACCOUNT", property = "account", jdbcType = JdbcType.VARCHAR)
        ,
        @Result(column = "PASSWORD", property = "password", jdbcType = JdbcType.VARCHAR)
        ,
        @Result(column = "NAME", property = "name", jdbcType = JdbcType.VARCHAR)
        ,
        @Result(column = "SEX", property = "sex", jdbcType = JdbcType.TINYINT)
        ,
        @Result(column = "DEPT_ID", property = "deptId", jdbcType = JdbcType.BIGINT)
        ,
        @Result(column = "CERT_TYPE", property = "certType", jdbcType = JdbcType.INTEGER)
        ,
        @Result(column = "CERT_NO", property = "certNo", jdbcType = JdbcType.VARCHAR)
        ,
        @Result(column = "TEL", property = "tel", jdbcType = JdbcType.VARCHAR)
        ,
        @Result(column = "MOBILE", property = "mobile", jdbcType = JdbcType.VARCHAR)
        ,
        @Result(column = "EMAIL", property = "email", jdbcType = JdbcType.VARCHAR)
        ,
        @Result(column = "CREATE_TIME", property = "createTime", jdbcType = JdbcType.TIMESTAMP)
        ,
        @Result(column = "EXP_TIME", property = "expTime", jdbcType = JdbcType.TIMESTAMP)
        ,
        @Result(column = "STATUS", property = "status", jdbcType = JdbcType.INTEGER)
        ,
        @Result(column = "LOGIN_NUM", property = "loginNum", jdbcType = JdbcType.VARCHAR)
        ,
        @Result(column = "REMARK", property = "remark", jdbcType = JdbcType.VARCHAR)
    })
    SysUser selectByPrimaryKey(Long id);

    @Select({
        "select",
        "ID, ACCOUNT, PASSWORD, NAME, SEX, DEPT_ID, CERT_TYPE, CERT_NO, TEL, MOBILE, ",
        "EMAIL, CREATE_TIME, EXP_TIME, STATUS, LOGIN_NUM, REMARK",
        "from sys_user",
        "where ACCOUNT = #{account}"
    })
    SysUser selectByAccount(String account);

    @SelectProvider(type = SysUserSqlProvider.class, method = "selectListByDto")
    List<SysUserDto> selectListByDto(SysUserDto dto);

    @Select({
        "select count(0)",
        "from sys_user",
        "WHERE DEPT_ID=#{deptId}"
    })
    int countByDepartmentId(long deptId);

    @Select({
        "select count(0)",
        "from sys_user_role",
        "where ROLE_ID=#{roleId}"
    })
    int countByRoleId(long roleId);

    @UpdateProvider(type = SysUserSqlProvider.class, method = "updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(SysUser record);

    @Update({
        "update sys_user",
        "set ACCOUNT = #{account,jdbcType=VARCHAR},",
        "PASSWORD = #{password,jdbcType=VARCHAR},",
        "NAME = #{name,jdbcType=VARCHAR},",
        "SEX = #{sex,jdbcType=TINYINT},",
        "DEPT_ID = #{deptId,jdbcType=BIGINT},",
        "CERT_TYPE = #{certType,jdbcType=INTEGER},",
        "CERT_NO = #{certNo,jdbcType=VARCHAR},",
        "TEL = #{tel,jdbcType=VARCHAR},",
        "MOBILE = #{mobile,jdbcType=VARCHAR},",
        "EMAIL = #{email,jdbcType=VARCHAR},",
        "CREATE_TIME = #{createTime,jdbcType=TIMESTAMP},",
        "EXP_TIME = #{expTime,jdbcType=TIMESTAMP},",
        "STATUS = #{status,jdbcType=INTEGER},",
        "LOGIN_NUM = #{loginNum,jdbcType=VARCHAR},",
        "REMARK = #{remark,jdbcType=VARCHAR}",
        "where ID = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(SysUser record);

    @Select({"SELECT ROLE_ID FROM sys_user_role WHERE USER_ID= #{id}"})
    List<Long> selectRoleIdByUserId(Long uid);
}
